package best.driver.android.View;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import best.driver.android.Activity.Order.Adapter.MultiAddressAdapter;
import best.driver.android.Beans.MultiAddressInfo;
import best.driver.android.Beans.OrderAddresses;
import best.driver.android.Beans.OrderStatus;
import best.driver.android.BestApp;
import best.driver.android.R;
import best.driver.android.Utils.SizeUtils;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import java.util.ArrayList;
import java.util.List;

public class MultiAddressView extends FrameLayout {

    private Context mContext;

    private List<OrderAddresses> mMultiAddressInfos = new ArrayList<OrderAddresses>();

    private MultiAddressAdapter mAdapter;

    @InjectView(R.id.tv_left)
    TextView mTvLeft;

    @InjectView(R.id.tv_right)
    TextView mTvRight;

    @InjectView(R.id.tv_right_detail)
    TextView mTvRightDetail;

    @InjectView(R.id.iv_show)
    ImageView mIvShow;

    @InjectView(R.id.tv_load_multi)
    TextView mTvLoadMulti;

    @InjectView(R.id.ll_address_one_line)
    LinearLayout mLLAddressOneLine;

    @InjectView(R.id.fl_multi_address)
    FrameLayout mFLMultiAddress;

    @InjectView(R.id.lv_multi_address)
    ListView mLvMultiAddress;

    @OnClick(R.id.iv_show)
    protected void showListView() {
        mLLAddressOneLine.setVisibility(View.GONE);
        mLvMultiAddress.setVisibility(View.VISIBLE);
    }

    public MultiAddressView(Context context) {
        this(context, null);
        mContext = context;
    }

    public MultiAddressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_multi_address, this, true);
        ButterKnife.inject(this, view);
        mAdapter = new MultiAddressAdapter(context);
        mLvMultiAddress.setAdapter(mAdapter);
        mAdapter.setData(mMultiAddressInfos);
    }

    public void setDate(List<OrderAddresses> infos) {
        this.mMultiAddressInfos = infos;
        if(infos != null && infos.size() > 0) {
            ViewGroup.LayoutParams params = mLvMultiAddress.getLayoutParams();
            float density = SizeUtils.getDensity(BestApp.getInstance().getApplicationContext());
            params.height = (int) (45 * density * infos.size() + 24 * density);
            mLvMultiAddress.setLayoutParams(params);
            mLvMultiAddress.setPadding(0, (int) (12 * density), 0, (int) (12 * density));
            OrderAddresses address = infos.get(infos.size() - 1);
            String pre = "提货地";
            if(OrderStatus.SHIPPER_ADDRESS.equals(infos.get(0).flag)) {
                address = infos.get(0);
            } else {
                pre = "卸货地";
            }
            if(infos.size() > 1) {
                mTvLeft.setText(pre + (infos.size()));
                if(OrderStatus.SHIPPER_ADDRESS.equals(address.flag)) {
                    mTvLeft.setText(pre + "1");
                }
                mTvRight.setText(address.address);
                mIvShow.setVisibility(View.VISIBLE);
                mTvLoadMulti.setVisibility(View.VISIBLE);
            } else {
                mTvLeft.setText(pre);
                mTvRight.setText(address.address);
                mIvShow.setVisibility(View.GONE);
                mTvLoadMulti.setVisibility(View.GONE);
            }
            if(!TextUtils.isEmpty(address.addressDetail)) {
                mTvRightDetail.setVisibility(View.VISIBLE);
                mTvRightDetail.setText(address.addressDetail);
            } else {
                mTvRightDetail.setVisibility(View.GONE);
            }
            mLLAddressOneLine.setVisibility(View.VISIBLE);
            mLvMultiAddress.setVisibility(View.GONE);
        }

        mAdapter.setData(this.mMultiAddressInfos);
    }



}
